<template>
  <p-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <p-form layout="inline" @keyup.enter.native="searchQuery">
        <p-row :gutter="24">
          <p-col :xl="10" :lg="11" :md="12" :sm="24">
            <p-form-item label="创建日期">
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" class="query-group-cust" v-model="queryParam.createTime_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间" class="query-group-cust" v-model="queryParam.createTime_end"></j-date>
            </p-form-item>
          </p-col>
          <p-col :xl="6" :lg="7" :md="8" :sm="24">
            <p-form-item label="产品名称">
              <p-input placeholder="请输入产品名称" v-model="queryParam.productName"></p-input>
            </p-form-item>
          </p-col>
          <template v-if="toggleSearchStatus">
            <p-col :xl="6" :lg="7" :md="8" :sm="24">
              <p-form-item label="制令类型">
                <j-dict-select-tag placeholder="请选择制令类型" v-model="queryParam.type" dictCode="product_type"/>
              </p-form-item>
            </p-col>
            <p-col :xl="6" :lg="7" :md="8" :sm="24">
              <p-form-item label="车间名称">
                <p-input placeholder="请输入车间名称" v-model="queryParam.workName"></p-input>
              </p-form-item>
            </p-col>
            <p-col :xl="6" :lg="7" :md="8" :sm="24">
              <p-form-item label="预计开工日">
                <j-date placeholder="请选择预计开工日" v-model="queryParam.startDate"></j-date>
              </p-form-item>
            </p-col>
          </template>
          <p-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <p-button type="primary" @click="searchQuery" icon="search">查询</p-button>
              <p-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</p-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <p-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </p-col>

        </p-row>
      </p-form>
    </div>
    <!-- 查询区域-END -->
    
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <p-button @click="handleAdd" type="primary" icon="plus">新增</p-button>
      <p-button type="primary" icon="download" @click="handleExportXls('生产工单')">导出</p-button>
      <p-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <p-button type="primary" icon="import">导入</p-button>
      </p-upload>




       <!-- 高级查询区域 -->
       <j-super-query :fieldList="fieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <p-dropdown v-if="selectedRowKeys.length > 0">
        <p-menu slot="overlay">
          <p-menu-item key="1" @click="batchDel"><p-icon type="delete"/>删除</p-menu-item>
        </p-menu>
        <p-button style="margin-left: 8px"> 批量操作 <p-icon type="down" /></p-button>
      </p-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>

        <span style="float:right;">
          <a @click="loadData()"><p-icon type="sync" />刷新</a>
          <p-divider type="vertical" />
          <p-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <p-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <p-row>
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                        <p-col :span="12"><p-checkbox :value="item.dataIndex">{{ item.title }}</p-checkbox></p-col>
                    </template>
                  </template>
                </p-row>
              </p-checkbox-group>
            </template>
            <a><p-icon type="setting" />自定义列</a>
          </p-popover>
        </span>
      </div>
        <div class="table-left">
                <p-tabs
                  defaultActiveKey="0"
                  tabPosition="left"
                  @change="handleTabsChange"
                  class="tabs-left">
                  <p-tab-pane tab="全部" key="0"></p-tab-pane>
                  <p-tab-pane v-for="i in dataCache" :tab="i.text" :key="i.value"></p-tab-pane>
                </p-tabs>
                <p-table
                ref="table"
                size="middle"
                bordered
                rowKey="id"
                :columns="columns"
                :dataSource="dataSource"
                :pagination="ipagination"
                :loading="loading"
                :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
                @change="handleTableChange"
                class="tabs-content">

                  <template slot="htmlSlot" slot-scope="text">
                    <div v-html="text"></div>
                  </template>
                  <template slot="imgSlot" slot-scope="text">
                    <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
                    <img v-else :src="getImgView(text)" height="25px" alt="图片不存在" style="max-width:80px;font-size: 12px;font-style: italic;"/>
                  </template>
                  <template slot="fileSlot" slot-scope="text">
                    <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
                    <p-button
                      v-else
                      :ghost="true"
                      type="primary"
                      icon="download"
                      size="small"
                      @click="uploadFile(text)">
                      下载
                    </p-button>
                  </template>

                  <span slot="action" slot-scope="text, record">
                    <a @click="handleEdit(record)">编辑</a>

                    <p-divider type="vertical" />
                    <p-dropdown>
                      <a class="ant-dropdown-link">更多 <p-icon type="down" /></a>
                      <p-menu slot="overlay">
                        <p-menu-item>
                          <p-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                            <a>删除</a>
                          </p-popconfirm>
                        </p-menu-item>
                      </p-menu>
                    </p-dropdown>
                  </span>

                </p-table>

              </div>
    </div>
    <!-- table区域-end -->

    <fxProductionOrders-modal ref="modalForm" @ok="modalFormOk"></fxProductionOrders-modal>
  </p-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
  import Vue from 'vue'
  import { filterObj } from '@/utils/util';
  import FxProductionOrdersModal from './modules/FxProductionOrdersModal'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import JDate from '@/components/jeecg/JDate.vue'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import { ajaxGetDictItems } from '@/api/api'



  //高级查询modal需要参数
    const superQueryFieldList=[
            {
                value:'createTime',
                text: '创建日期',
                type: 'Date'
            },
            {
                value:'productName',
                text: '产品名称',
                type: 'String'
            },
            {
                value:'type',
                text: '制令类型',
                type: 'select',
                dictCode: 'product_type'
            },
            {
                value:'status',
                text: '状态',
                type: 'select',
                dictCode: 'product_status'
            },
            {
                value:'workName',
                text: '车间名称',
                type: 'String'
            },
            {
                value:'startDate',
                text: '预计开工日',
                type: 'Date'
            },
            {
                value:'endDate',
                text: '预计完工日',
                type: 'Date'
            },
            {
                value:'remark',
                text: '备注',
                type: 'String'
            },
    ]

  export default {
    name: "FxProductionOrdersList",
    mixins:[JeecgListMixin],
    components: {
      JSuperQuery,
      JDictSelectTag,
      JDate,
      FxProductionOrdersModal
    },
    data () {
      return {
        fieldList:superQueryFieldList,
        superQueryFlag:false,
        superQueryParams: '',
        description: '生产工单管理页面',
        dataCache: [],
        // 表头
        columns: [
        ],
        //列设置
        settingColumns:[],
        //列定义
        defColumns: [
          {
            title: "序号",
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align: 'center',
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'创建日期',
            align: 'center',
            dataIndex: 'createTime'
          },
          {
            title:'产品名称',
            align: 'center',
            dataIndex: 'productName'
          },
          {
            title:'制令类型',
            align: 'center',
            dataIndex: 'type_dictText'
          },
          {
            title:'状态',
            align: 'center',
            dataIndex: 'status_dictText'
          },
          {
            title:'车间名称',
            align: 'center',
            dataIndex: 'workName'
          },
          {
            title:'预计开工日',
            align: 'center',
            dataIndex: 'startDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'预计完工日',
            align: 'center',
            dataIndex: 'endDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'备注',
            align: 'center',
            dataIndex: 'remark'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: '/codedemoapi/feixuns/fxProductionOrders/list',
          delete: '/codedemoapi/feixuns/fxProductionOrders/delete',
          deleteBatch: '/codedemoapi/feixuns/fxProductionOrders/deleteBatch',
          exportXlsUrl: '/codedemoapi/feixuns/fxProductionOrders/exportXls',
          importExcelUrl: '/codedemoapi/feixuns/fxProductionOrders/importExcel',
        },
        dictOptions:{},
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${this.url.importExcelUrl}`;
      }
    },
    created() {
      this.initColumns();
    },

    methods: {
      initColumns(){
        //权限过滤（列权限控制时打开，修改第二个参数为授权码前缀）
        //this.defColumns = colAuthFilter(this.defColumns,'testdemo:');

        var key = this.$route.name+":colsettings";
        let colSettings= Vue.ls.get(key);
        if(colSettings==null||colSettings==undefined){
          let allSettingColumns = [];
          this.defColumns.forEach(function (item,i,array ) {
            allSettingColumns.push(item.dataIndex);
          })
          this.settingColumns = allSettingColumns;
          this.columns = this.defColumns;
        }else{
          this.settingColumns = colSettings;
          const cols = this.defColumns.filter(item => {
            if(item.key =='rowIndex'|| item.dataIndex=='action'){
              return true;
            }
            if (colSettings.includes(item.dataIndex)) {
              return true;
            }
            return false;
          })
          this.columns =  cols;
        }
      },
      //列设置更改事件
      onColSettingsChange (checkedValues) {
        var key = this.$route.name+":colsettings";
        Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
        this.settingColumns = checkedValues;
        const cols = this.defColumns.filter(item => {
          if(item.key =='rowIndex'|| item.dataIndex=='action'){
            return true
          }
          if (this.settingColumns.includes(item.dataIndex)) {
            return true
          }
          return false
        })
        this.columns =  cols;
      },
        handleTabsChange(key) {
            let that = this;
            key === '0' ? key = '' : key
            that.queryParam.status=key;
            that.loadData();
        },
        //高级查询方法
        handleSuperQuery(arg) {
            if(!arg){
                this.superQueryParams=''
                this.superQueryFlag = false
            }else{
                this.superQueryFlag = true
                this.superQueryParams=JSON.stringify(arg)
            }
            this.loadData()
        },
        // 获取查询条件
        getQueryParams() {
            let sqp = {}
            if(this.superQueryParams){
                sqp['superQueryParams']=encodeURI(this.superQueryParams)
            }
            var param = Object.assign(sqp, this.queryParam, this.isorter);
            param.field = this.getQueryField();
            param.pageNo = this.ipagination.current;
            param.pageSize = this.ipagination.pageSize;
            return filterObj(param);
        },
      initDictConfig(){
          ajaxGetDictItems("product_status", null).then((res) => {
            if (res.success) {
              console.log(res.data);
              this.dataCache = res.data;
            }
          })
      },

    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>